package com.kehao.leetcode.jzoffer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/7/5 13:54
 */
public class JZSolution046 {
    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private void dfs(TreeNode node,Map<Integer,Integer> levelToRightVal,int level){
        if(node==null)return;
        levelToRightVal.put(level,node.val);
        dfs(node.left,levelToRightVal,level+1);
        dfs(node.right,levelToRightVal,level+1);
    }

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Map<Integer,Integer> levelToRightVal = new HashMap<>();
        dfs(root,levelToRightVal,0);
        for (int i = 0; i < levelToRightVal.size(); i++) {
            result.add(levelToRightVal.get(i));
        }
        return result;
    }
}
